// pages/page/main2/main2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,//是否自动切换banner
    xianshiHour: "00",
    xianshiMin: "00",
    xianshiSen: "00",

    jinShengTimeLongAll: 0,
    bannerInfoArray: ['http://tea.teapaopao.com/img/20180109/default/374e9fd976f8ed6489cd1462e4f0cdf3.jpg?imageView2/2/w/700',
      'http://tea.teapaopao.com/img/20171206/default/07596bcb11634445897dc304b88a689c.jpg?imageView2/2/w/700',
      'http://tea.teapaopao.com/img/20180122/default/32942059a816fdb1cd9ecd179d18241a.jpg?imageView2/2/w/700',
      'http://tea.teapaopao.com/img/20171207/default/43df53cc08a67ea2a548566cd30c9268.jpg?imageView2/2/w/700'
    ],
    currentBannerSelected: 0,
    indicatorDots: false,
    color_fffff: "#FFFFFF",
    color_4A4A4A: "#4A4A4A",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDataFromService();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      autoplay: true,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      autoplay: false,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉刷新");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上拉加载");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   *请求后台首页数据
   */
  getDataFromService: function () {
    var that = this;
    wx.request({
      url: "http://120.55.137.143:54321/service/teabubble/shop_paopao_home?methods=getCPPHomePageInfo",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {

      },
      success: function (res) {
        console.log(res.data);

        that.setJinShenInfo(res.data.content.limitTimeInfo);//设置显示特惠的信息
        // that.setBanner(res.data.content.);//设置banner视图

        that.setExplosionGoods(res.data.content.explosionGoodsList);//设置新店疯抢
        that.setThemeActivity(res.data.content.themeActivityList);//设置主题视图

      }
    })
  },
  /**
   * 设置主题视图
   */
  setThemeActivity: function (value) {
    this.setData({
      themeActivityList: value,
    })
  },
  /**
   * 设置新店疯抢
   */
  setExplosionGoods: function (value) {
    this.setData({
      explosionGoodsList: value,
    });
  },

  /**
   * 设置banner视图
   */
  setBanner: function (bannerInfo) {
    this.setDta({
      bannerInfoArray: bannerInfo,
    })
  },
  /**
   * 设置限时特惠的信息
   */
  setJinShenInfo: function (limitTimeInfo) {
    var that = this;
    var i = 0, len=0;
    for (i = 0, len = limitTimeInfo.goodsList.length; i < len; i++) {
      limitTimeInfo.goodsList[i].goodListPriceRes = (limitTimeInfo.goodsList[i].goodsPrice / limitTimeInfo.goodsList[i].discountRate).toFixed(2);//四舍五入取两位小数
    }
    that.setData({
      jinShengInfo: limitTimeInfo,
    })

    // 先执行一次，然后在循环执行
    that.showJinShenTime(limitTimeInfo, that.data.jinShengTimeLongAll, null);
    var jinShengTimeInterval = setInterval(function () {
      that.showJinShenTime(limitTimeInfo, that.data.jinShengTimeLongAll, jinShengTimeInterval);
    }, 1000);


  },
  /**
   * 设置限时特惠的时间显示
   */
  showJinShenTime: function (limitTimeInfo, jinShengTimeLong, jinShengTimeInterval) {
    var endTimeStr = limitTimeInfo.endTime;//结束时间
    var startTimeStr = limitTimeInfo.startTme;//开始时间
    var serverTimeStr = limitTimeInfo.serverTime;//服务器的时间
    // /*    /-/g 是正则表达式 表示将所有短横线 - 替换为斜杠 / 其中g表示全局替换   */
    // endTimeStr = endTimeStr.replace(/-/g, "/");
    // startTimeStr = startTimeStr.replace(/-/g, "/");
    // serverTimeStr = serverTimeStr.replace(/-/g, "/");
    var endTime = new Date(endTimeStr);
    var startTime = new Date(startTimeStr);
    var serverTime = new Date(serverTimeStr);
    //判断剩余时间，如果时间为0，则不再降低时间，并且将试图关闭

    //计算仅剩时间，仅剩时间=结束时间-服务器时间
    if (jinShengTimeLong == 0) {
      jinShengTimeLong = endTime.getTime() - serverTime.getTime();
    } else {
      jinShengTimeLong = jinShengTimeLong - 1000;
    }
    if (jinShengTimeLong <= 0) {
      clearInterval(jinShengTimeInterval);
    }

    var hoursLong = jinShengTimeLong / 1000 / 60 / 60 % 24;
    var minLong = (jinShengTimeLong / 1000 / 60) % 24;
    var secLong = (jinShengTimeLong / 1000) % 24;

    var hours = parseInt(hoursLong).toString();
    var min = parseInt(minLong).toString();
    var sec = parseInt(secLong).toString();

    if (hours.length < 2) {
      hours = "0" + hours;
    }
    if (min.length < 2) {
      min = "0" + min;
    }
    if (sec.length < 2) {
      sec = "0" + sec;
    }
    this.setData({
      jinShengTimeLongAll: jinShengTimeLong,
      xianshiHour: hours,
      xianshiMin: min,
      xianshiSen: sec,
    })
  },

  /**
  * 用户点击搜索方法
  */
  serachClick: function (event) {
    wx.showToast({
      title: '点击了搜索',
    })
  },
  /**
   * 首页轮播图的监听器
   */
  mainPageBannerChangeListener: function (event) {
    this.setData({
      currentBannerSelected: event.detail.current,
    });
  },
  /**
   * 点击跳转其他页面
   */
  navigateTo: function (event) {
    console.log("跳转的连接---->" + event.currentTarget.dataset.url);
    var navToUrl = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: navToUrl,
      
    })
  }

})